package com.hxh.tess4j.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author huangxunhui
 * Date: Created in 2019-04-16 10:40
 * Utils: Intellij Idea
 * Description:
 */
public class ImageUtil {


    /**
     * 移除背景
     * @param filePath 文件路径
     * @param outFile 输出文件路径
     * @throws IOException 异常
     */
    public static void removeBackground(String filePath , String outFile) throws IOException {

        //定义一个临界阈值
        int threshold = 500;

        // 获取图片
        File imageFile = new File(filePath);

        BufferedImage img =  ImageIO.read(imageFile);
        int width = img.getWidth();
        int height = img.getHeight();

        for (int x = 0; x < width; x++){
            for (int y = 0; y < height; y++){
                Color color = new Color(img.getRGB(x, y));
                int num = color.getRed()+color.getGreen()+color.getBlue();
                if(num >= threshold){
                    img.setRGB(x, y, Color.BLACK.getRGB());
                }
            }
        }

        for(int i = 1;i<width;i++){
            Color color1 = new Color(img.getRGB(i, 1));
            int num1 = color1.getRed()+color1.getGreen()+color1.getBlue();
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Color color = new Color(img.getRGB(x, y));

                    int num = color.getRed()+color.getGreen()+color.getBlue();
                    if(num==num1){
                        img.setRGB(x, y, Color.WHITE.getRGB());
                    }else{
                        img.setRGB(x, y, Color.BLACK.getRGB());
                    }
                }
            }
        }

        File file = new File(outFile);
        ImageIO.write(img, "jpg", file);
    }


    public static void  binaryImage(String filePath , String outFile) throws IOException {
        File file = new File(filePath);
        BufferedImage image = ImageIO.read(file);

        int width = image.getWidth();
        int height = image.getHeight();

        //重点，技巧在这个参数BufferedImage.TYPE_BYTE_BINARY
        BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
        for(int i= 0 ; i < width ; i++){
            for(int j = 0 ; j < height; j++){
                int rgb = image.getRGB(i, j);
                grayImage.setRGB(i, j, rgb);
            }
        }

        File newFile = new File(outFile);
        ImageIO.write(grayImage, "jpg", newFile);
    }

    public static void grayImage(String filePath , String outFile) throws IOException {
        File file = new File(filePath);
        BufferedImage image = ImageIO.read(file);

        int width = image.getWidth();
        int height = image.getHeight();

        //重点，技巧在这个参数BufferedImage.TYPE_BYTE_GRAY
        BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        for(int i= 0 ; i < width ; i++){
            for(int j = 0 ; j < height; j++){
                int rgb = image.getRGB(i, j);
                grayImage.setRGB(i, j, rgb);
            }
        }

        File newFile = new File(outFile);
        ImageIO.write(grayImage, "jpg", newFile);
    }



    public static void main(String[] args) throws IOException {
        String filePath = "/Users/code/IdeaProjects/tess4j/src/main/resources/img/code.jpg";
        String outPathBinary = "/Users/code/IdeaProjects/tess4j/src/main/resources/img/code_1.jpg";
        String outPathGray = "/Users/code/IdeaProjects/tess4j/src/main/resources/img/code_2.jpg";

        binaryImage(filePath,outPathBinary);
        grayImage(filePath,outPathGray);
    }
}
